package com.xyebank.ledger.service.xyd.adaper;

import java.util.Date;

import com.xyebank.ledger.util.DateConverter;
import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LRepaymentInf;
import com.xyebank.ledger.service.common.conveter.RePayBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateUtil;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.vo.deal.XYDCashRepayVo;

@Component(value = "XYDRepayBeanConverter")
public class XYDRepayBeanConverter implements RePayBeanConverter<XYDCashRepayVo> {

    ModelMapper modelMapper = new ModelMapper();

	// 不能采取注入方式
	private DateConverter dateConverter = new DateConverter();

    class NXApplyPropertyMap extends PropertyMap<XYDCashRepayVo, LRepaymentInf> {
        // 注意 不要有重复的映射，不然会报错
        protected void configure() {
            // 本期应还本金
            // map(source.getPrincipal(), destination.getPayPrincipal());
            // 本期还款时间
//            map(source.getPaidofftime(), destination.getRepayTime());
            // 本期剩余结清金额
            // map(source.getInstallmentnotpaid(), destination.getRepayAmount());
            // 放款状态
            // map(source.getStatus(), destination.getStatus());
            // 订单号
            map(source.getOutorderNumber(), destination.getXyOrderNo());
            // 电话号码
            map(source.getprimaryPhone(), destination.getMblNo());
        }
    }

    public XYDRepayBeanConverter() {
        modelMapper.addMappings(new NXApplyPropertyMap());
    }

    @Override
    public LRepaymentInf covert(XYDCashRepayVo x) {
        LRepaymentInf entity = modelMapper.map(x, LRepaymentInf.class);
        entity.setDataSource(x.getOutorderNumber().length() == 16 ? CommonConstant.JRY_STR : CommonConstant.SJD_STR);
        entity.setXyOrgNo(ProductConstant.XYD_LOAN_ORG);
        entity.setXyProductNo(ProductConstant.XYD_LOAN_PROD_ID);
        entity.setId(IDUtil.generateId(x.getId().toString(), "xyd_repay"));
        entity.setPayPrincipal(Double.parseDouble("null".equals(x.getPrincipal()) ? "0" : x.getPrincipal()));
        entity.setRepayAmount(Double.parseDouble("-".equals(x.getInstallmentnotpaid()) ? "0" : x.getInstallmentnotpaid()));
        entity.setRepayTime("-".equals(x.getPaidofftime()) ? "" : x.getPaidofftime());
        String status = x.getStatus();
        if ("-1".equals(status)) {
            entity.setStatus(CommonConstant.REPAY_OVER);
        } else if ("0".equals(status)) {
            entity.setStatus(CommonConstant.REPAY_UNCLEAR);
        } else if ("1".equals(status)) {
            entity.setStatus(CommonConstant.REPAY_CLEAR);
        } else if ("2".equals(status)) {
            entity.setStatus(CommonConstant.REPAY_OVER_CLEAR);
        }

        return entity;
    }
}
